package com.sxbbc.plat.controller.jz;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.https.HttpUtil;
import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.enums.ParamEnums;
import com.sxbbc.common.core.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.utils.statics.FunctionUtils;
import com.sxbbc.plat.common.MappingUtils;
import com.sxbbc.plat.util.SystemLog;

/**
 * 系统信息设置控制层
 *
 * @author leehao
 * @date 2019-04-28 16:29
 */
@Controller
public class SystemController {

    @Autowired
    private IBaseInfoService baseInfoService;
    @Autowired
    private ISysAgreementService sysAgreementService;
    @Autowired
    private IInfoShoppingService infoShoppingService;
    @Autowired
    private IPayService payService;
    @Autowired
    private IInfoParamService infoParamService;
    @Autowired
    private BBusinessService bBusinessService;
    @Autowired
    private ICConfigurationService icConfigurationService;
    @Autowired
    private IAppVersionService appVersionService;


    /**
     * 系统管理-基础信息页面
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = MappingUtils.SHOW_BASE_INFOS)
    public ModelAndView showAllBaseInfo(Integer tabIndex) {
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_BASE_INFOS);
        // 基础配置
        BaseInfo baseInfo = baseInfoService.getById(1);
        view.addObject("baseInfo", baseInfo);
        // 协议列表
        List<SysAgreement> list = sysAgreementService.list(null);
        view.addObject("agreeList", list);
        // 购物配置信息
        InfoShopping shop = infoShoppingService.getById(1);
        view.addObject("shop", shop);
        // 支付配置信息
        Map<String,Object> map = payService.queryPayInfo();
        view.addObject("wxPay",map.get("wxPay"));
        view.addObject("aliPay",map.get("aliPay"));

        // 基础配置信息
        List<InfoParam> paramList = infoParamService.list();
        view.addObject("paramList", paramList);

        //分发平台地址
        AppVersion version = appVersionService.getOne(new QueryWrapper<>());
        view.addObject("version",version);

        view.addObject("baseId", 1);
        view.addObject("tabIndex",tabIndex);
        return view;
    }

    /**
     * 保存网站基础信息
     * @param pcLogo pclogo图片
     * @param appLogo applogo图片
     * @param andUrl 安卓二维码
     * @param iosUrl ios二维码
     * @param wxUrl 微信二维码
     * @param info baseInfo对象
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = MappingUtils.EDIT_BASE_INFO)
    @ResponseBody
    @SystemLog(methods = "保存网站基础信息", module = "系统管理")
    public RestResponse editSysBaseInfo(@RequestParam(name = "pcLogoImage", required = false) MultipartFile pcLogo,
                                        @RequestParam(name = "appLogoImage", required = false) MultipartFile appLogo,
                                        @RequestParam(name = "andUrl", required = false) MultipartFile andUrl,
                                        @RequestParam(name = "iosUrl", required = false) MultipartFile iosUrl,
                                        @RequestParam(name = "wxUrl", required = false) MultipartFile wxUrl, BaseInfo info) {
        return baseInfoService.editSysBaseInfo(pcLogo, appLogo, andUrl, iosUrl, wxUrl, info);
    }

    /**
     * 新增/编辑协议页面
     * @param type 协议类型
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = MappingUtils.SHOW_EDIT_AGREEMENT)
    public ModelAndView showEditAgreement(Integer type,Integer index) {
        SysAgreement agree = sysAgreementService.getById(type);
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_EDIT_AGREEMENT);
        view.addObject("agree", agree);
        view.addObject("index",index);
        return view;
    }

    /**
     * 新增/编辑协议
     * @param agree 对象
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = MappingUtils.EDIT_AGREEMENT)
    @ResponseBody
    @SystemLog(methods = "新增编辑系统协议", module = "系统管理")
    public RestResponse doEditAgree(SysAgreement agree) {
        return sysAgreementService.editAgree(agree);
    }

    @RequestMapping(value = MappingUtils.DELETE_AGREEMENT)
    @ResponseBody
    @SystemLog(methods = "删除系统协议",module = "系统管理")
    public boolean deleteAgreement(Integer id){
        return sysAgreementService.removeById(id);
    }

    /**
     * 保存购物配置
     * @param info
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = MappingUtils.EDIT_SHOPINFO)
    @ResponseBody
    @SystemLog(methods = "保存购物配置信息", module = "系统管理")
    public RestResponse doEditShopInfo(InfoShopping info) {
        return infoShoppingService.editShopInfo(info);
    }

    /**
     * 保存微信支付配置
     * @param wxInfos
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = MappingUtils.EDIT_PAYINFO_WX)
    @ResponseBody
    @SystemLog(methods = "保存微信支付配置信息",module = "系统管理")
    public RestResponse doEditWxPayInfo(InfoWx wxInfos){
        return payService.editWxPayInfo(wxInfos);
    }

    /**
     * 保存支付宝支付配置
     * @param infoAli
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = MappingUtils.EDIT_PAYINFO_ALI)
    @ResponseBody
    @SystemLog(methods = "保存支付宝支付配置信息",module = "系统管理")
    public RestResponse doEditAliPayInfo(InfoAli infoAli){
        return payService.editAliPayInfo(infoAli);
    }


    /**
     * 保存版本信息
     * @param appVersion
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = MappingUtils.EDIT_APPVERSION)
    @ResponseBody
    @SystemLog(methods = "保存版本信息",module = "系统管理")
    public RestResponse doEditAppVersion(AppVersion appVersion){
        return appVersionService.editAliPayInfo(appVersion);
    }

    @RequestMapping(value = MappingUtils.UPLOAD_RICH_FILE)
    @ResponseBody
    public Object uploadFile(@Param("uploadImage") MultipartFile upfile){
        return FunctionUtils.uploadFile(upfile);
    }
    
    /**
     * 编辑基础信息
     * @param paramId
     * @return ModelAndView
     * @author xiaoky
     */
    @GetMapping(value = MappingUtils.SHOW_PARAM_EDIT)
    public ModelAndView showEditParam (Integer paramId) {
    	InfoParam param = infoParamService.getById(paramId);
    	ModelAndView view = new ModelAndView(MappingUtils.SHOW_PARAM_EDIT);
    	view.addObject("infoParam", param);
    	return view;
    }
    
    /**
     * 编辑基础配置
     * @param param
     * @return 编辑结果
     * @author xiaoky
     */
    @RequestMapping(value = MappingUtils.DO_EDIT_PARAM)
    @ResponseBody
    public RestResponse doEditParam (InfoParam param) {
    	if (StringUtils.isEmpty(param.getKvalue())) {
    		return GetRest.getFail("请输入kvalue");
    	}
        BBusiness business = new BBusiness();
    	CConfiguration cConfiguration = new CConfiguration();
    	if (param.getId() != null) {
            //对商家抽成比例改变时，统一改变b_business的scale字段
            if (StringUtils.equals(param.getKcode(),ParamEnums.BUSINESS_SCALE.getKey())) {
                business.setScale(new BigDecimal(param.getKvalue()));
            }
    	}
    	boolean saveOrUpdate = infoParamService.saveOrUpdate(param);
    	if (saveOrUpdate) {
            if (StringUtils.equals(param.getKcode(), ParamEnums.BUSINESS_SCALE.getKey())) {
                saveOrUpdate = bBusinessService.update(business,null);
                if (!saveOrUpdate) {
                    throw new ThrowJsonException("更改商家抽成比例失败");
                }
            }
    		return GetRest.getSuccess("保存成功");
    	}
    	return GetRest.getFail("保存失败");
    }
    
    /**
     * 删除基础信息配置数据
     * @param paramId 数据id
     * @return 处理结果
     * @author xiaoky
     */
    @RequestMapping(value = MappingUtils.DELETE_PARAM)
    @ResponseBody
    public RestResponse deleteParam (Integer paramId) {
    	boolean delete = infoParamService.removeById(paramId);
    	if (delete) {
    		return GetRest.getSuccess("删除成功");
    	}
    	return GetRest.getFail("删除失败");
    }

    @RequestMapping(value = MappingUtils.HANDLE_CLEAR)
    @ResponseBody
    public RestResponse handleClear(){
        try {
            HttpUtil.getInstance().get("http://api.jfh6688.cn/base/refresh");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return GetRest.getSuccess("");
    }
}
